package com.wanxi.service.impl;

import com.wanxi.dao.RoleDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.Role;
import com.wanxi.service.RoleService;
import com.wanxi.util.CreateOnlyID;
import com.wanxi.util.JsonCopy;
import com.wanxi.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleDao roleDao;

    @Autowired
    CreateOnlyID createOnlyID;
    @Override
    public ResultDTO findAllRole() {
        List<Role> roleList = roleDao.findAll();
        return ResultDTO.getDTO(roleList.size(),roleList);
    }

    @Override
    public ResultDTO addRole(RoleVO roleVO) {
        Role role = JsonCopy.copy(roleVO, Role.class);
        Long roleId = createOnlyID.createId();
        role.setId(roleId);
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        role.setEnable(1);
        int i = roleDao.insert(role);
        if (i==1){
            if(Objects.nonNull(roleVO.getPermissionIds())){
                for (Long permissionId :
                        roleVO.getPermissionIds()) {
                    Long id = createOnlyID.createId();
                    roleDao.addPermissionOfRole(id,roleId,permissionId);
                }
            }
            return ResultDTO.getDTO(i,roleVO);
        }
        return ResultDTO.builder().msg("error").code(1).count(0).data(roleVO).build();
    }

    @Override
    public ResultDTO deleteRole(RoleVO roleVO) {
        Role role = JsonCopy.copy(roleVO, Role.class);
        if(role.getId()!=1){
            int i = roleDao.deleteById(role);
            if(i==1){
                roleDao.deletePermissionOfRole(role);
                return ResultDTO.getDTO(i,roleVO);
            }
        }
        return ResultDTO.builder().msg("error").code(1).count(0).data(roleVO).build();
    }

    @Override
    public ResultDTO findRoleById(RoleVO roleVO) {
        Role role = JsonCopy.copy(roleVO, Role.class);
        Role r= roleDao.findById(role);
        if(Objects.nonNull(r)){
            return ResultDTO.getDTO(1,r);
        }
        return ResultDTO.builder().msg("error").code(1).count(0).data(roleVO).build();
    }

    @Override
    public ResultDTO updateRole(RoleVO roleVO) {
        Role role = JsonCopy.copy(roleVO, Role.class);
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        role.setEnable(1);
        if(Objects.nonNull(role.getId())){
            int i = roleDao.updateById(role);
            if(i==1){
                roleDao.deletePermissionOfRole(role);
                if(Objects.nonNull(roleVO.getPermissionIds())){
                    for (Long permissionId :
                            roleVO.getPermissionIds()) {
                        Long id = createOnlyID.createId();
                        roleDao.addPermissionOfRole(id, role.getId(),permissionId);
                    }
                }
                return ResultDTO.getDTO(i,roleVO);
            }
        }
        return ResultDTO.builder().msg("error").code(1).count(0).data(roleVO).build();
    }
}
